package com.jt.resource.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author 金帮嘉诚 @date 2021-08-26 星期四 10:14
 */
@Slf4j
@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.jt.resource.annotation.RequiredLog)")
    public void doLog(){}//锦上添花

    //以下这种写法只适用于一个切入点只写一个切面方法的情况下
//    @Around("@annotation(com.jt.resource.annotation.RequiredLog)")
    @Around("doLog()")//只有该注解能在目标方法前后都添加语句
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.debug("Before {}",System.currentTimeMillis());//{}是占位符
        Object result = joinPoint.proceed();//执行执行链
        //后续获取登录用户信息
        Object principal =
                SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        System.out.println("principal = " + principal);


        log.debug("After {}",System.currentTimeMillis());
        return result;//目标方法(切入点方法)的执行结果
    }
}
